<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN"
        "http://www.w3.org/TR/1999/REC-html401-19991224/frameset.dtd">
<%@ page 
  language="java"
  import="java.util.Vector,  
  com.epro.standard.frontend.util.FileInfoUtil,
  com.epro.standard.frontend.lov.*,
  com.softplus.carrefour.hd.bltier.entity.*,
  com.softplus.carrefour.hd.bltier.admin.UserLevelManage,
  com.softplus.carrefour.hd.CFHDSystem,
  com.softplus.carrefour.hd.bltier.util.*,
  com.epro.standard.frontend.util.FileInfoUtil,
  java.util.StringTokenizer,
  org.displaytag.tags.TableTag"             
%>
<html xmlns:epro>
<%@ taglib uri="/WEB-INF/HeadTag.tld" prefix="head"%>  
<%@ taglib uri="/WEB-INF/ToolbarTag.tld" prefix="toolbar"%>
<%@ taglib uri="/WEB-INF/TextfieldTag.tld" prefix="textfield"%>
<%@ taglib uri="/WEB-INF/ListboxTag.tld" prefix="listbox"%>
<%@ taglib uri="/WEB-INF/TextareaTag.tld" prefix="textarea"%>
<%@ taglib uri="/WEB-INF/displaytag-12.tld" prefix="display"%>
<%@ taglib uri="/WEB-INF/ErrorTag.tld" prefix="error"%>
<head:Head
	author="Pomme"
	import="textfield,listbox,textarea,toolbar,masterTable,error"
	styleFile="../theme/defaultTheme.css,../theme/CFRLTheme.css"
	jsFile="../script/checkValidData.js,../script/Alert.js,../script/dateCalendar.js,../script/convertDataType.js,../script/multiRecLovControl.js,../script/lov.js,../script/masterTable.js"
	title="User Level">
</head:Head>
<%-- @include file="../include/eproFileInfo.jsp" --%>
<%  
  System.out.println("==================== BEGIN JSP CFHDADMT01=================");
  com.epro.standard.frontend.util.EproServletUtil servletUtil = new com.epro.standard.frontend.util.EproServletUtil(request) ;	
  
  String backURL		= "/CFRL/CFRLWEBMN01.jsp?page=1" ;
  String backClick		= "parent.location='" + backURL + "'" ; 
   
  //Get user level 
  String level = "";
  //level = request.getParameter("level") == null ? "Select" :  servletUtil.getRequestValue("level");
  level = request.getParameter("level") != null ? servletUtil.getRequestValue("level") : request.getParameter("back_level") != null ? request.getParameter("back_level") : "Select"  ;

  System.out.println("=============================User Level ="+level);  
  
	String queryURL						  = "CFHDADMT01.jsp?uid=" + servletUtil.getRequestValue("uid") + "&level=" + level ;
	String queryOnclick					= "window.location.href('" + queryURL +"')";

  //Get UserID From LOV
  String userFromLOV = "";

  userFromLOV = servletUtil.getRequestValue("userID") ;
  System.out.println("This............................. = " + userFromLOV);

  System.out.println("============================userFromLOV ===== " + userFromLOV);
  Vector vecFromLOV = new Vector();
  
   //Seperate User ID To vecFromLOV
   if (userFromLOV != "")
   {
    StringTokenizer st =new StringTokenizer(userFromLOV, ",")	;
  
    while(st.hasMoreElements())
    {
        vecFromLOV.addElement(st.nextToken()) ;
    }   
  }
  
  //Create Vector From Lov To Show 
  Vector userForShow = new Vector();
  UserLevelManage uManage = new UserLevelManage();
  UserLevel ul =null;
  
  LOVKeyword key = new LOVKeyword();
  Vector vecCheckBox = new Vector();
  key.setCheckBox(vecCheckBox);
  String countryID = "" ;
  String countryFromLOV = "" ;

  if (!vecFromLOV.equals(""))
  {
    countryID = servletUtil.getRequestValue("countryID") ;
    for(int i=0; i<=vecFromLOV.size()-1; i++)
    {
      String user = (String)vecFromLOV.elementAt(i);
                     
      key.setMessage(user);
      Vector result = new Vector();
      result = uManage.getLOVuserID(key);
     
      EproLOVRow er = (EproLOVRow)result.elementAt(0);
      Vector detail = er.getLOVRow();
      System.out.println("=========================== detail == " + detail.size()) ;
      //out.println("LOV = " + detail);
      String user_id = (String)detail.elementAt(0);
      String user_name = (String)detail.elementAt(1);
      String user_surname = (String)detail.elementAt(2);
      String role_name = (String)detail.elementAt(3);
      String country_name = (String)detail.elementAt(4);
      countryFromLOV += (String)detail.elementAt(5) + "," ;
      
      UserAccount userAccount = new UserAccount();
      userAccount.setUserId(user_id);
      userAccount.setUserName(user_name);
      userAccount.setUserSurname(user_surname);
      
      Role role = new Role();
      role.setRoleName(role_name);
      
      Country country = new Country();
      country.setCountryName(country_name);
      
      userAccount.setRole(role);
      userAccount.setCountry(country);
      
      UserLevel uLevel = new UserLevel();
      uLevel.setUserId(user_id);
      uLevel.setUserAccount(userAccount);
      
      userForShow.add(uLevel);
    }
  } 
    
  // variable for master table need	*********	
	String   masterWidth                   = "500";
	String   masterHeight 	                = "310";
   
  //Query User From User Level 
  UserLevelManage ulManage =(UserLevelManage)CFHDSystem.getControlClass("USER_LEVEL");
    
  Vector userInLevel = new Vector();
  if(!(level.equals("Select")))
  {
    userInLevel = ulManage.getUserByLevel(level);
    if (userInLevel.size() > 0)
      countryID = ((UserLevel)userInLevel.elementAt(0)).getUserAccount().getCountry().getCountryId() ;
  }
  System.out.println(">>>>><<<<<<<<<<<<"+userInLevel);
  
  Vector CheckDeleteButton = new Vector();
  CheckDeleteButton = userInLevel;
  
  //Show When Return From LOV
  if(userForShow.size() > 0)
  {
    userInLevel = userForShow;
  }
  request.setAttribute( "menuv", userInLevel);
  System.out.println("POMME=============LEVEL=================" + level);
%>
    <body onfocus="checkMultipleLov(frm2,frm2.userID)">
<div id="processingLayer" style="visibility:hidden; position:absolute; left:100px; top:150px;">
	<IMG alt="Waiting..." SRC="../images/background/Processing.gif" WIDTH="585" HEIGHT="136" BORDER="0">
</div>
<div style="width:100%;height:90%;margin:5px" id="programLayer">
    <form name = "frm" action="../servlet/com.softplus.carrefour.hd.frontend.servlet.administration.CFHDAD01" method="post">
      <input type="hidden" name="action" value="" >
      <input type="hidden" name="userToSave" value="<%=userFromLOV%>">    
      <input type="hidden" name="back_level" value="<%=level%>">      
      <table align="center" border="0" width="100%" cellpadding="0" cellspacing="0">
        <tr>
          <td valign="top" align="left" style="padding-left:15px;">
               <span style="width:100pt;">&nbsp;</span>
        <img src= "../images/icon/toolbar/nBackToMenu.gif" alt="Back To Menu" onclick="Javascript:backTomenu();" style="cursor:hand"
              onmouseover="Javascript: this.src='../images/icon/toolbar/oBackToMenu.gif' " 
              onmouseout="Javascript: this.src='../images/icon/toolbar/nBackToMenu.gif' ">      
            <%if((CheckDeleteButton.size() > 0)||(vecFromLOV.size() > 0)){%> 
            <img alt="Query" src="../images/icon/toolbar/nQuery.gif" onmouseover="this.src='../images/icon/toolbar/oQuery.gif'" onmouseout="this.src='../images/icon/toolbar/nQuery.gif'" style="cursor:hand" onclick="queryData();">
             <%}%>    
             
            <%if (!(level.equals("Select"))){%>              
            <img src= "../images/icon/toolbar/nAdd.gif" alt="Add" onclick="popUpLov()" style="cursor:hand"
              onmouseover="Javascript: this.src='../images/icon/toolbar/oAdd.gif' " 
              onmouseout="Javascript: this.src='../images/icon/toolbar/nAdd.gif' ">                        
              <%}%>
             
             <%if((CheckDeleteButton.size() > 0)||(vecFromLOV.size() > 0)){%> 
             <img src= "../images/icon/toolbar/nDelete.gif" alt="Delete" onclick="ToDelete()" style="cursor:hand"
              onmouseover="Javascript: this.src='../images/icon/toolbar/oDelete.gif' " 
              onmouseout="Javascript: this.src='../images/icon/toolbar/nDelete.gif' ">  
             <%}%>
              
              <%if(userForShow.size() > 0){%>
              <img src= "../images/icon/toolbar/nSave.gif" alt="Save" onclick="ToSave()" style="cursor:hand"
              onmouseover="Javascript: this.src='../images/icon/toolbar/oSave.gif' " 
              onmouseout="Javascript: this.src='../images/icon/toolbar/nSave.gif' ">
              <%}%>
          </td>
        </tr>
        <tr>
          <td align="center" style="padding-left:15px;">
          <%if (userInLevel.size() > 0) {%>
            <div class="tableContainer"  id="tableContainer" style="height:355px;">                     
            <display:table  name="menuv" id="menuv" styleClass="scrollTable"  cellspacing="0" width="100%">
           
              <display:column title="<input type=\"checkbox\" name=\"deleteCheckAll\" alt=\"select \ unselect all\" onclick=\"checkAllBox('user_id')\" >" nowrap="true" style="width:5pt;" headerStyleClass=" fixedHeader" styleClass="scrollContent">
              <input type="checkbox" value="<%=  ((UserLevel)menuv).getUserId()  %>" name="user_id<%//= pageContext.getAttribute("menuv_rowNum")%>"   onclick="checkEachBox('user_id','deleteCheckAll')">
              </display:column>
            
              <display:column title="User ID" nowrap="true" sortable="true" style="width:67pt;"  headerStyleClass=" fixedHeader" styleClass="scrollContent">
                <%= ((UserLevel)menuv).getUserId() %>
              </display:column>
               
              <display:column title="User Name" nowrap="true" sortable="true" maxLength="50" headerStyleClass=" fixedHeader" styleClass="scrollContent">
                <%= ((UserLevel)menuv).getUserAccount().getUserName() %>
              </display:column>
              
              <display:column title="User Surname" nowrap="true" sortable="true" maxLength="50" headerStyleClass=" fixedHeader" styleClass="scrollContent">
                <%= ((UserLevel)menuv).getUserAccount().getUserSurname() %>
              </display:column>
              
               <display:column title="Role Name" nowrap="true" sortable="true" style="width:72pt;" maxLength="30" headerStyleClass=" fixedHeader" styleClass="scrollContent">
                <%= ((UserLevel)menuv).getUserAccount().getRole().getRoleName() %>
              </display:column>
               
              <display:column title="Country Name" nowrap="true" sortable="true" style="width:80pt;" headerStyleClass=" fixedHeader" styleClass="scrollContent">
                <%= ((UserLevel)menuv).getUserAccount().getCountry().getCountryName()%>
              </display:column>
              
            </display:table>                
            </div>
            <%}%>
          </td>
        </tr>
      </table>      
    </form>
    <form name = "frm2" action="<%= request.getRequestURI()%>" method="post">      
       <input type="hidden" name="userID" value="">
       <input type="hidden" name="back_level" value="<%=level%>">
       <input type="hidden" name="countryID" value="<%= countryID %>">
    </form>
</div>
 <script type="text/javascript">  
  
    function levelSelected()
    {   
      document.location.href = document.location.pathname+"?level="+frm.userLevel.value;
    }
    
    function ToDelete()
    {
      if (checkBeforeDelete('user_id'))
      {
        if (showOConfirmDialog("Confirm","Do you want to delete?","OK","Cancel") == 1)
        {
          if (<%=userFromLOV.equals("")%>)
          {
            document.all.frm.action.value="delete";
            frm.submit();
          }
          else DeleteLOV() ;
        }
      }
      else
      {
        showOWarningDialog('No item selected','Please select item before continue','OK') ;
      }
    }
      
    function replaceAll(srcStr, oldStr, repStr) {
      var returnValue = "";
      var tempValues = srcStr.split(oldStr);
  
      for (var i=0; i<tempValues.length; i++) {
        if (i != tempValues.length-1) {
          returnValue += tempValues[i]+repStr;
        } else {
          returnValue += tempValues[i];
        }
      }
      return returnValue;
    }
    
    function DeleteLOV()
    {
        frm2.userID.value = getRemainID(false) ;
        frm2.submit() ;
    }
    
    function getRemainID(hasQuate)
    {
        var checkBoxNode  = document.getElementsByName('user_id') ;
        var remainingUserID = '' ;
        if (checkBoxNode != undefined)
        {
          for(var i=0;i<checkBoxNode.length;i++)
          {
            if (!(checkBoxNode(i).checked))
            {
              userID = document.all.menuv.children[1].children[i].children[1].innerText ;
              if (hasQuate)
                remainingUserID += ",'" + replaceAll(replaceAll(userID,' ',''),'\r\n','') + "'" ;
              else
                remainingUserID += "," + replaceAll(replaceAll(userID,' ',''),'\r\n','') ;
            }
          }
        }
        return remainingUserID!=''?remainingUserID.substring(1):'' ;
    }
    
    function queryData()
    {
      if (<%=!userFromLOV.equals("")%>)
      {
             if (showOConfirmDialog('Save Data', 'Do you want to save the changes?', 'OK', 'Cancel') == 1)
             {
                    document.all.frm.action.value="insert";
                    frm.submit();
             }
             else{
                    window.location.href ="<%= queryURL%>" ;
            }
      }else{
               window.location.href ="<%= queryURL%>" ;
      }

    }
    
    function backTomenu()
    {
        showProgramLayer();
           if (<%=!userFromLOV.equals("")%>)
           {
              if (showOConfirmDialog('Save Data', 'Do you want to save the changes?', 'OK', 'Cancel') == 1)
              {
                canSave = true ;
                if ('L2Z,L3BKK,PROD'.indexOf('<%= level%>') >= 0)
                  canSave = checkSameCountry() ;
                if (canSave)
                {
                  document.all.frm.action.value="insert";
                  frm.submit();
                }
                else
                {
                  showOErrorDialog("Error","Please select users from the same country for this Level.","OK") ;
                }
              }
              else{
                <%= backClick %>;
              }
           }
          else{
                <%= backClick %>;
          }
    }
    
    function checkSameCountry()
    {
      var countryArr = '<%= countryFromLOV%>'.split(',') ;
      isAllSame = (countryArr.length > 1)
      if (isAllSame)
      {
        for(i=0; i<countryArr.length-1 ;i++)
        {
          isAllSame = (countryArr[i] == countryArr[0])
          if (!isAllSame)
            break ;
        }
      }
      return isAllSame ;
    }
    function ToSave()
    {
      //alert('delete!!');
      canSave = true ;
      if ('L2Z,L3BKK,PROD'.indexOf('<%= level%>') >= 0)
        canSave = checkSameCountry() ;
      if (canSave)
      {
        if (showOConfirmDialog("Confirm","Do you want to save?","OK","Cancel") == 1)
        {
          document.all.frm.action.value="insert";
          frm.submit();
         }
      }
      else
      {
        showOErrorDialog("Error","Please select users from the same country for this Level.","OK") ;
      }
    }
    
    function popUpLov()
    {
      remainID = '' ;
      selectedID = '' ;
      if (<%=!userFromLOV.equals("")%>)
      {
        remainID = frm2.userID.value ;
        selectedID = getRemainID(true) ;
      }
      paramValue = '' ;
      if ('<%= countryID %>' != '')
        paramValue = '&countryID=<%= countryID %>' ;
      userLevel = '<%= level%>' ;
      showLov('../CFHDLOV/CFHDLOV01USER_LEVEL01.jsp?isMultiple=true&userLevel=' + userLevel + '&selectedID=' + selectedID + paramValue,'frm2.userID','', '');
      frm2.userID.value = (remainID==''?'':',') + frm2.userID.value ;
    }
    //check multiple Lov selection
  	function checkMultipleLov(frmName , inputObj){
		if(inputObj.value!="")
		{
        remainID = getRemainID(false) ;
        if (<%=!userFromLOV.equals("")%>)
        {
          if (frm2.userID.value.indexOf(remainID)<0)
            frm2.userID.value = remainID==''?'':',' + remainID + ',' + frm2.userID.value ;
        }        
				frm2.submit();        
		}
	}

    	function showProgramLayer(){
            document.all.programLayer.style.visibility='hidden';
            document.all.processingLayer.style.visibility='visible';
	}
  </script>    
  </body>
  </html>
  <%@ include file="../include/eproErrorAlert.jsp"%>
